package com.das.mechanic_base.mvp.view.create.fragment;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.create.SelectWorkBean;

import java.util.ArrayList;
import java.util.List;

public class X3AddServiceAlreadySelectedWorkAdapter extends RecyclerView.Adapter<X3AddServiceAlreadySelectedWorkAdapter.X3AddServiceWorkHolder> {

    private List<SelectWorkBean.ServiceBaseEntityBean> mList;

    private List<SelectWorkBean.ServiceBaseEntityBean> sList;

    private Context mContext;

    public X3AddServiceAlreadySelectedWorkAdapter(Context mContext, List<SelectWorkBean.ServiceBaseEntityBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
        this.sList = new ArrayList<>();
    }

    public void changeAddService(List<SelectWorkBean.ServiceBaseEntityBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public X3AddServiceWorkHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new X3AddServiceWorkHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_already_add_service_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull X3AddServiceWorkHolder holder, int position) {

        holder.iv_add.setImageResource(R.mipmap.x3_service_cute_icon);

        holder.tv_name.setText(mList.get(position).name);

        holder.itemView.setOnClickListener(view -> {
            String tag = (String) holder.iv_add.getTag();
            SelectWorkBean.ServiceBaseEntityBean variantList = mList.get(position);
                if (iOnSelectWorkList != null) {
                    iOnSelectWorkList.iOnSelectWork(variantList,true);
                }
        });

    }

    IOnSelectWorkList iOnSelectWorkList;

    public void setiOnSelectWorkList(IOnSelectWorkList iOnSelectWorkList) {
        this.iOnSelectWorkList = iOnSelectWorkList;
    }

    public interface IOnSelectWorkList {
        void iOnSelectWork(SelectWorkBean.ServiceBaseEntityBean serviceBean,boolean isDel);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class X3AddServiceWorkHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        ImageView iv_add;
        View v_padding;
        public X3AddServiceWorkHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            iv_add = itemView.findViewById(R.id.iv_add);
            v_padding = itemView.findViewById(R.id.v_padding);
        }
    }

    /**
     * 获取组名
     */
    public String getGroupName(int position){

        String sortLetters = mList.get(position).getSortLetters();

        return   sortLetters;
    }

    public boolean isGroupHead(int position) {
        if (position >= mList.size()) {
            return false;
        }
        if (position == 0) {
            return true;
        } else {
            String currentGroup = getGroupName(position);
            String preGroup = getGroupName(position - 1);
            return !currentGroup.equals(preGroup);
        }
    }

}
